<template>
	<view class="settlement-page-box">

		<scroll-view class="settlement-scroll-box" scroll-y>
			<view class="settlement-scroll-content">

				<!-- 订单状态轴 -->
				<view class="order-procedure">
					<view class="state-axis">
						<view class="axis-round round-active"></view>
						<view class="axis-line" v-if="details.advance_type == 2"></view>
						<view class="axis-round" v-if="details.advance_type == 2"></view>
						<view class="axis-line"></view>
						<view class="axis-round"></view>
						<view class="axis-line"></view>
						<view class="axis-round"></view>
					</view>
					<view class="state-txts">
						<view class="state-txt txt-active">{{details.advance_type == 1?'付全款':"付定金"}}</view>
						<view class="state-txt" v-if="details.advance_type == 2">付尾款</view>
						<view class="state-txt">配送/自取</view>
						<view class="state-txt">交易完成</view>
					</view>
				</view>
				<view class="settlement-list-box">
					<view class="settlement-list">
						<view class="settlement-list-time flex">
							<view style="color: #999">营业时间:</view>
							<view class="settlement-list-item">
								<view style="margin-left: 14rpx;font-size: 26rpx;" v-for="(item,index) in business_time"
									:key="index">{{item[0]}}-{{item[1]}};</view>
							</view>
						</view>
					</view>
					<view class="settlement-list" v-if="pickup_type==2">
						<view class="settlement-list-time flex">
							<view style="color: #999">配送时间:</view>
							<view class="settlement-list-item">
								<view style="margin-left: 14rpx;font-size: 26rpx;" v-for="(item,index) in send_time"
									:key="index">{{item[0]}}-{{item[1]}};</view>
							</view>
						</view>
					</view>

					<!-- 配送方式 -->
					<view class="settlement-list">
						<view class="settlement-list-title">
							<text>配送方式</text>
						</view>
						<view class="settlement-list-content">
							<view class="ettlement-list-content-column">
								<block v-for="(item,index) in pickup_list" :key="item.id">
									<view class="settlement-list-choose" @tap="sendMethod" :data-index="item.id">
										<text
											:style="'color:' + (pickup_type==item.id?'#000':'#999')">{{item.id==1?('到店'+item.name):item.name}}</text>
										<image
											:src="pickup_type==item.id?'/static/images/choose-circle-1.png':'/static/images/choose-circle-2.png'">
										</image>
									</view>
								</block>
							</view>

						</view>
					</view>
					<view class="settlement-list" v-if="pickup_type==1||pickup_type==2">
						<view class="settlement-list-title">
							<text>时间选择</text>
						</view>
						<view class="settlement-list-choose">
							<!-- #ifndef MP-ALIPAY -->
							<picker class="settlement-time-picker timer-picker-color" mode="multiSelector"
								@change="bindStartMultiPickerChange" @tap="pickerTap"
								@columnchange="bindMultiPickerColumnChange" :value="multiIndex" :range="multiArray">
								{{startDate==''?('点击选择'+(pickup_type==2?'起送':'自提')+'时间'):startDate}}
							</picker>
							<!-- #endif -->
							<!-- #ifdef MP-ALIPAY -->
							<uni-popup type="bottom" ref="popup" ::mask-click="false" :is-mask-click="false"
								style="background: rgba(255,255,255,1);" class="popup">
								<view class="operate">
									<view @click="successSelect" class="sure">完成</view>
								</view>
								<picker-view indicator-style="{height: 100rpx}"
									class="settlement-time-picker timer-picker-color" :value="multiIndex"
									@click="pickerTap" @change="bindStartMultiPickerChange"
									style="background: rgba(255,255,255,1);">
									<picker-view-column v-for="(col,index) in  multiArray" :key="index">
										<view class="item" v-for="(item,i) in col" :key="i">{{item}}</view>
									</picker-view-column>
								</picker-view>
							</uni-popup>
							<view class="select-time" @click="showSelectTime">
								{{startDate==''?('点击选择'+(pickup_type==2?'起送':'自提')+'时间'):startDate}}
							</view>
							<!-- #endif -->
							<image class="settlement-list-choose-right" src="/static/images/arrow-right-1.png"></image>
						</view>
					</view>

				</view>

				<view class="settlement-list-box" v-if="blessinfo.status==1" style="padding:0">
					<view v-if="blessData.url" class="zhufu_bg_now">
						<view class="blesstitle">
							<text class="blessttext">{{blessData.type==1?'视频':'录音'}}</text>
							<text class="blessttext" style="color:#ccc"
								v-if="blessData.length">时长:{{blessData.length + 's'}}</text>
						</view>
						<view class="play_icon">
							<image class="play_pic" v-if="isPlaying" @click="toStop"
								src="../../../static/images/audio_play_end.png"></image>
							<image class="play_pic" @click="toPlay(blessData.type)" v-else
								src="../../../static/images/audio_play_start.png"></image>
							<view>
								<uni-icons @click="toEdit(blessData.type)" type="gear-filled" size="16" color="#ccc">
								</uni-icons>修改
								<uni-icons @click="deleteItem(blessData.type)" style="margin-left:20rpx"
									type="trash-filled" size="16" color="#ccc"></uni-icons>删除
							</view>
						</view>
					</view>
					<view class="zhufu_bg" v-else @click="toBlessing">
						<image style="width:100%;height:100%" :src="blessinfo.entrance_style"></image>
						<!-- <image style="width:100%;height:100%" src="https://lscs.mobxcx.com/Public/images/blessing_default.png"></image> -->
					</view>
				</view>

				<view class="settlement-list-box">
					<!-- 引入选择收货地址模板 -->
					<block v-if="pickup_type == 2 || pickup_type == 3">
						<view class="settlement-list" v-if="isShowAddress">
							<address-diy :address="address"></address-diy>
						</view>
						<view class="settlement-list">
							<block v-if="pickup_type==2">
								<view class="settlement-distance">范围: {{math/1000}}km</view>
								<view class="settlement-distance">距离: {{distance}}km</view>
							</block>
							<view v-else></view>
							<view class="settlement-list-price-box" v-if="isShowFee == 1">
								<text class="settlement-list-small-text">配送费:</text>
								<text class="settlement-list-price">￥{{delivery_fee}}</text>
							</view>
							<view class="settlement-list-price-box" style='text-decoration: line-through;'
								v-if="isShowFee == 2">
								<text class="settlement-list-small-text">配送费:</text>
								<text class="settlement-list-price">￥{{delivery_fee}}</text>
							</view>
						</view>
						<view v-if="delivery_status == 1">
							<express-fee :delivery_cost="delivery_cost"></express-fee>
						</view>
					</block>
					<block v-if="pickup_type == 1">
						<view class="settlement-list flexStart">
							<text>姓名</text>
							<input type="text" :value="link_name" @input="inputName" placeholder-class="pl"
								placeholder="联系人姓名"></input>
						</view>
						<view class="settlement-list flexStart">
							<text>手机号</text>
							<input type="number" :value="mobile_phone" maxlength="11" @input="inputPhone"
								placeholder-class="pl" placeholder="联系人手机号"></input>
						</view>
					</block>
				</view>
				<view class="settlement-list-box">
					<view class="settlement-list" v-if="vip && userInfo.platform_type == 1">
						<view class="settlement-vip-box">
							<view class="settlement-vip">
								<text>会员卡：{{vip.card_num}}</text>
								<text>折扣：{{vip.discount? (vip.discount*100)/10 :""}}</text>
							</view>
							<view class="settlement-vip">
								<text>余额：{{vip.balance}}</text>
								<text>{{integral_name}}：{{vip.integral}}</text>
							</view>
						</view>
					</view>
					<block :key="index">
						<view class="settlement-list flexStart" @tap.stop="toGoodsDetail"
							:data-goods_id="details.goods_id">
							<view class="settlement-goods-img">
								<image mode="aspectFit" :src="details.goods_img"></image>
							</view>
							<view class="settlement-goods-con">
								<view class="settlement-goods-name">{{details.goods_name}}</view>
								<view class="settlement-goods-sku" v-if="details.sku_name">{{details.sku_name}}</view>
								<view class="settlement-goods-num">
									<text>x</text>
									<text>{{details.num}}</text>
								</view>
							</view>
							<view class="settlement-goods-price">
								<text>￥{{details.advance_price}}</text>
							</view>
						</view>
					</block>
				</view>
				<view class="settlement-list-box">
					<view class="settlement-list" @tap="couponFun">
						<view class="settlement-list-title">
							<text>优惠券</text>
						</view>
						<view class="settlement-list-choose">
							<text class="settlement-list-price" v-if="coupon">-{{coupon.reduce_money}}元</text>
							<text v-if="!coupon">选择优惠券</text>
							<image class="settlement-list-choose-right" src="/static/images/arrow-right-1.png"></image>
						</view>
					</view>
					<view class="settlement-list">
						<view class="settlement-distance" @tap.stop="toShowOrHidedisrule">
							<image src="/static/images/rule.png"></image>
							<text>计价规则</text>
						</view>
						<view>
							<text>已优惠</text>
							<text class="settlement-list-price">￥{{coupon?coupon.reduce_money:0}}</text>
						</view>
						<view>
							<text>小计</text>
							<text class="settlement-list-price">￥{{allMoney}}</text>
						</view>
					</view>
				</view>
				<!-- 订单支付阶段 -->
				<view class="payment-stage" v-if="details.advance_type == 2">
					<view class="stage-one">
						<view class="stage-left">
							<image src="/static/images/check-1.png"></image>
							<view>阶段一: 定金</view>
						</view>
						<view class="stage-right">￥{{details.earnest_price}}</view>
					</view>
					<view class="stage-middle"></view>
					<view class="stage-two">
						<view class="stage-left">
							<image src="/static/images/check-2.png"></image>
							<view>阶段二: 尾款</view>
						</view>
						<view v-if="details.arrive_price > 0" class="stage-arrive">
							<text>定金抵现:</text>
							<text>￥{{details.arrive_price}}</text>
						</view>
						<view class="stage-right">￥{{tailPrice}}</view>
					</view>
					<view class="stage-tip">尾款支付时间 {{details.tail_time}}</view>
				</view>
				<!-- 同意不退定金 -->
				<view v-if="details.refuse_earnest_price==1" class="agree-list">
					<view>我同意预售商品不退定金</view>
					<image @tap="chooseRefuseEarnest"
						:src="refuse_earnest_price == 0?'/static/images/stage-choiced.png':'/static/images/stage-choice.png'">
					</image>
				</view>
				<view class="settlement-list-box">
					<view class="settlement-list">
						<text>给卖家留言</text>
						<text class="more-word">最多100字</text>
					</view>
					<textarea maxlength="100" placeholder="留言" v-model="remark" class="textarea-t" :always-embed="true"
						placeholder-class="textarea-p" cursor-spacing="0"></textarea>

				</view>
			</view>
		</scroll-view>
		<!-- 底部 -->
		<view class="settlement-footer">
			<view class="settlement-footer-left">
				<view class="settlement-footer-left-all">
					<text>{{details.advance_type == 1?'付全款':'付定金'}}:</text>
					<text>￥{{payMoney}}</text>
				</view>
			</view>
			<view @click="payBtn" class="report-submit">
				<view class="settlement-pay-btn" v-if="canClick">提交订单</view>
				<view class="settlement-pay-btn settlement-pay-btn-dis" v-else>提交订单</view>
			</view>
		</view>


		<!-- 优惠规则 -->
		<view class="settlement-model" catchtouchmove="myCatchTouch" v-if="showdisrule">
			<view class="settlement-model-inner settlement-model-inner-height">
				<icon color="#999" type="cancel" @tap.stop="toShowOrHidedisrule"></icon>
				<view class="settlement-model-title">
					<text>计价规则</text>
				</view>
				<view class="settlement-rule">
					<view>1、活动商品不参加打折，活动优惠为商品原价减活动价。</view>
					<view>2、会员价商品计价方式为会员价×折扣，四舍五入到分(0.01元，不足1分算1分)，再乘以数量。</view>
					<view>3、普通商品计价方式为单价×折扣，四舍五入到分(0.01元，不足1分算1分)，再乘以数量。</view>
					<view>4、优惠券抵扣为计算其他优惠后、再计算优惠券满减。</view>
					<view>5、订单最终支付最少1分(0.01元)完成下单购买。</view>
				</view>
			</view>
		</view>


		<view class="livePlay" v-if="videoShow">
			<view class="closeIcon">
				<uni-icons style="margin-right:30rpx" type="close" size="18" @click="closeVedio"></uni-icons>
				<!-- <uni-icons type="download" size="18" @click="uploadVedio"></uni-icons> -->
			</view>
			<video id="myVideo" style="width: 100%;height: 95%;" :src="blessData.url" controls
				enable-play-gesture></video>
		</view>
	</view>
</template>

<script>
	// pages/advanceSettlement/advanceSettlement.js
	var app = getApp().globalData;
	const innerAudioContext = uni.createInnerAudioContext();
	const videoContext = uni.createVideoContext('myVideo')
	const recorderManager = uni.getRecorderManager();
	import addressDiy from '@/components/address-diy.vue'
	import expressFee from '@/components/express-fee.vue'
	
	export default {
		data() {
			return {
				startDate: "",
				choose_send_time: '',
				//选择的配送时间
				choose_pick_time: '',
				//选择的自提时间
				multiArray: [
					['今天', '明天', '3-2', '3-3', '3-4', '3-5'],
					[0, 1, 2, 3, 4, 5, 6],
					[0, 10, 20]
				],
				//时间选择器里面的值
				multiIndex: [0, 0, 0],
				//被选中的时间下标
				address: {},
				vip: '',
				pickup_type: 0,
				//1自提 2同城配送  3快递发货
				canClick: false,
				link_name: '',
				mobile_phone: null,
				distance: 0,
				delivery_fee: 0,
				//配送费
				payMoney: 0,
				//最终支付金额
				showTextarea: false,
				textFocus: false,
				textPadding: 0,
				remark: '',
				showdisrule: false,
				business_state: 2,
				//关闭非营业时间下单
				business_time: "",
				isPaySuccess: false,
				delivery_set: [0, 0, 0],
				refuse_earnest_price: 0,
				//用户是否同意不退定金
				tailPrice: 0,
				//尾款
				allMoney: 0, //总额
				details: "",
				coupon: "",
				send_time: '',
				cacul_time: [],
				math: '',
				starting_price: '',
				isShowAddress: true,
				delivery_status: 0,
				delivery_cost: '',
				isShowFee: 1,
				pickup_list: [],
				imageValue: '', // 祝福语image
				blessinfo: {
					status: '0', // 权限
					entrance_style: '',
					privacy_statement: '',
				}, // 祝福语info
				blessData: {
					saveName: '',
					type: '',
					url: '',
					length: 0,
					storage_type: '',
				},
				videoShow: false,
				powerLevel: '', // 配送等级
				entry_num_privacy: 0,
				blessing_status: false, // 祝福语权限
				isPlaying: false,
				send_delayed_time: 0,
				pickup_delayed_time: 0,
				auto_set_time: 1,
				userInfo: {},
				integral_name:''
			};
		},

		components: {
			addressDiy,
			expressFee
		},
		props: {},

		watch: {
			'blessinfo': {
				immediately: true,
				deep: true,
				handler: function(val) {
					console.log(val)
				}
			}
		},
		/**
		 * 生命周期函数--监听页面加载
		 */
		onLoad: function(options) {
			var that = this;
			var details = uni.getStorageSync('advanceDetails');
			const userInfo = uni.getStorageSync('userInfo')
			this.setData({
				details: details,
				userInfo: userInfo ? userInfo : {}
			});

			if (getApp().globalData.XcxData.systemInfo.platform == 'ios') {
				this.setData({
					textPadding: '10rpx'
				});
			}

			uni.removeStorageSync('shoppingAddress');
			uni.removeStorageSync('useCoupon');
			uni.removeStorageSync('blessData')
			this.getBuyData(details)
		},

		/**
		 * 生命周期函数--监听页面初次渲染完成
		 */
		onReady: function() {},

		/**
		 * 生命周期函数--监听页面显示
		 */
		onShow: function() {
			this.integral_name = this.$store.integral_name;
			var address = uni.getStorageSync('shoppingAddress');
			var coupon = uni.getStorageSync('useCoupon');
			let params = uni.getStorageSync('blessData')

			if (params) {
				params = JSON.parse(params)
				this.$set(this.blessData, 'saveName', params.url);
				this.$set(this.blessData, 'storage_type', params.storageType);
				this.$set(this.blessData, 'type', params.type);
				this.$set(this.blessData, 'url', params.address);
				this.$set(this.blessData, 'length', params.audio_length);
			}

			this.setData({
				coupon: coupon ? coupon : '',
				isShowAddress: true,
				blessData: this.blessData
			});


			if (address) {
				this.setData({
					address: address
				});
				this.range(function() {
					that.calculateAllMoney();
				});
			} else {
				this.calculateAllMoney();
			}



			this.$forceUpdate()

			var that = this;
			getApp().globalData.sendRequest({
				url: '/Pay/getUserDetail',
				type: "post",
				showLoading: false,
				data: {},
				success: function(res) {
					if (res.status == 1) {
						var obj = {
							vip: res.vip_data
						};
					} else {
						var obj = {
							vip: null
						};
					}

					that.setData(obj);
				}
			});

		},

		/**
		 * 生命周期函数--监听页面隐藏
		 */
		onHide: function() {
			this.setData({
				isShowAddress: false
			})
		},

		/**
		 * 生命周期函数--监听页面卸载
		 */
		onUnload: function() {
			uni.removeStorageSync('shoppingAddress');
			uni.removeStorageSync('useCoupon');
			console.log(this.isPaySuccess);

			if (!this.isPaySuccess && this.order_no) {
				var that = this;
				uni.showModal({
					title: '提示',
					content: '确认取消该订单？',
					showCancel: true,
					cancelText: '取消订单',
					confirmText: '待会儿付',
					confirmColor: '#ff5c6d',
					success: function(res) {
						if (!res.confirm) {
							getApp().globalData.sendRequest({
								url: '/Order/cancelPay',
								data: {
									order_no: that.order_no
								},
								success: function(res) {}
							});
						}
					}
				});
			}
		},
		methods: {
			getBuyData(details) {
				let that = this
				getApp().globalData.sendRequest({
					url: '/Advance/get_pay_info',
					type: "post",
					data: {
						id: details.id
					},
					success: async (res)=> {
						if (res.status == 1) {
							if (res.data.storeSet.blessing_status) {
								// 获取祝福语权限
								that.getBlessPower()
							}
							console.log(res.data.storeSet)
							var obj = {
								send_time: res.data.storeSet.new_business,
								business_time: res.data.storeSet.new_business_time,
								math: res.data.storeSet.service_radius,
								starting_price: res.data.storeSet.starting_price,
								business_state: res.data.storeSet.business_non_order,
								delivery_set: res.data.storeSet.delivery_set
							};

							obj.link_name = res.data.link_name;
							obj.mobile_phone = res.data.mobile_phone;
							let result = await that.sendPowerLevel(res.data.storeSet.delivery_set)
							obj.pickup_type = result
							that.canClick = true
							that.send_delayed_time = res.data.storeSet.send_delayed_time
							that.pickup_delayed_time = res.data.storeSet.pickup_delayed_time
							if ((obj.pickup_type == 2 || obj.pickup_type == 3) && !res.data.address) {
								that.canClick = false
							}
							let address = that.address.id ? that.address : (res.data.address ? res.data.address : {});
							uni.setStorageSync('shoppingAddress', Object.keys(address).length != 0?address:'')
							that.setData({
								send_time: res.data.storeSet.new_business,
								business_time: res.data.storeSet.new_business_time,
								math: res.data.storeSet.service_radius,
								starting_price: res.data.storeSet.starting_price,
								business_state: res.data.storeSet.business_non_order,
								delivery_set: res.data.storeSet.delivery_set,
								link_name: res.data.link_name,
								mobile_phone: res.data.mobile_phone,
								address: address,
								pickup_type: obj.pickup_type,
								delivery_status: res.delivery_status,
								delivery_cost: res.delivery_cost,
								auto_set_time: res.data.storeSet.auto_set_time
							});

							// 自动选择时间
							that.auto_set_time == 1 && await that.selectTime()

							if (Object.keys(this.address).length != 0 && (that.pickup_type == 2 || that.pickup_type == 3)) {
								that.range(function() {
									that.calculateAllMoney();
								});
							} else {
								if (that.vip) {} else {
									that.calculateAllMoney();
								}
							}
						}
					}
				});

			},


			// 同意退定金
			chooseRefuseEarnest() {
				var refuse_earnest_price = this.refuse_earnest_price;
				this.setData({
					refuse_earnest_price: !refuse_earnest_price
				});
			},


			// 修改选择时间
			selectTime() {
				let date_params = {
					detail: {
						value: [0, 0, 0]
					}
				}
				this.pickerTap()
				this.bindStartMultiPickerChange(date_params)
			},

			//点击显示规则
			toShowOrHidedisrule: function() {
				var showdisrule = this.showdisrule;
				this.setData({
					showdisrule: !showdisrule
				});
			},
			//点击留言
			showRemark: function() {
				var that = this;
				this.setData({
					textFocus: true,
					showTextarea: true
				});
			},
			//输入留言
			inputRemark: function(e) {
				this.setData({
					remark: e.detail.value
				});
			},
			//留言框失焦
			textBlur: function() {
				this.setData({
					textFocus: false,
					showTextarea: false
				});
			},
			//输入自提姓名
			inputName: function(e) {
				this.setData({
					link_name: e.detail.value
				});
			},
			//输入自提手机号
			inputPhone: function(e) {
				let telReg = /^1[3456789]\d{9}$/

				if (!telReg.test(e.detail.value)) {
					this.canClick = false
					return;
				} else {
					this.setData({
						mobile_phone: e.detail.value,
						canClick: true
					});

				}
			},

			// 配送等级权限
			sendPowerLevel(delivery_set) {
				console.log(delivery_set)
				const that = this
				let pickup_type = 0
				return new Promise((resolve) => {
					getApp().globalData.sendRequest({
						url: '/MerInterface/storeConfigInfo',
						success: function({
							status,
							message,
							data
						}) {
							console.log(data)
							if (status == 1) {
								if (!data || !data.distribution_json) {
									that.pickup_list = [{
											name: '快递配送',
											id: 3
										},
										{
											name: '同城配送',
											id: 2
										},
										{
											name: '自提',
											id: 1
										},
									]

								} else {
									that.pickup_list = JSON.parse(data.distribution_json)
								}
								that.pickup_list = that.pickup_list.filter(x => {
									return (delivery_set[x.id - 1] > 0)
								})
								pickup_type = that.pickup_list[0].id
								resolve(pickup_type)
							} else {
								uni.showToast({
									title: message,
									icon: 'none'
								})
							}
						}
					})

				})

			},


			// 上传祝福语
			toBlessing() {
				if (this.entry_num_privacy == 0) {
					this.entry_num_privacy++
					uni.navigateTo({
						url: '../../order/privacyStatement/privacyStatement?info=' + this.blessinfo
							.privacy_statement
					})
				} else {
					uni.navigateTo({
						url: '../../order/selectBlessingWays/selectBlessingWays'
					})
				}

			},

			// 编辑
			toEdit(type) {
				type == 2 ? this.toStop() : this.closeVedio()
				let blessData = uni.getStorageSync('blessData')
				if (blessData) {
					blessData = Object.assign(JSON.parse(blessData), {
						flag: 'isEdit'
					})
				}
				uni.navigateTo({
					url: '../../order/selectBlessingWays/selectBlessingWays?blessData=' + JSON.stringify(blessData)
				})
			},

			// 关闭视频
			closeVedio() {
				videoContext.stop()
				this.videoShow = false
			},


			// 删除
			deleteItem(type) {
				type == 1 ? this.closeVedio() : this.toStop()
				let that = this
				uni.showModal({
					title: '提示',
					content: '确定要删除祝福吗?',
					success: async function(res) {
						if (res.confirm) {
							let obj = {
								storage_type: that.blessData.storage_type,
								url: that.blessData.saveName
							}
							try {
								const res = await this.$request('Blessing/Applets/deleteBlessing', obj)
								if (res.status == 1) {
									uni.showToast({
										title: '删除成功',
									})
									that.videoShow = false
									that.blessData = {}
								}
								uni.removeStorageSync('blessData')
							} catch (err) {
								console.log(err)
							}
						}
					}
				})

			},

			// 播放
			toPlay(type) {
				let _this = this
				// 视频
				if (type == 1) {
					this.videoShow = true
					videoContext.play()
				} else {
					console.log(type, 11)
					// 录音
					_this.isPlaying = true

					innerAudioContext.src = _this.blessData.url;
					console.log(innerAudioContext.src)
					innerAudioContext.play()
					innerAudioContext.onPlay(() => {
						innerAudioContext.onTimeUpdate(() => {
							console.log(innerAudioContext.duration);
							_this.blessData.length = innerAudioContext.duration
						})
						setTimeout(() => {
							console.log(innerAudioContext.duration)
							_this.blessData.length = innerAudioContext.duration
						}, 1000)
					})
					innerAudioContext.onEnded(() => {
						_this.isPlaying = false
					})
				}
			},

			// 播放暂停
			toStop() {
				innerAudioContext.pause()
				this.isPlaying = false
			},


			// 获取祝福语权限
			async getBlessPower() {

				try {
					const {
						data,
						status
					} = await this.$request('Blessing/Applets/privacyStatement')
					if (status == 1) {
						this.blessinfo = Object.assign({}, data)
					} else {
						uni.showToast({
							title: '获取祝福语权限失败',
							icon: 'none'
						})
					}
				} catch (err) {
					console.log(err)
				}
			},


			//选择配送方式
			sendMethod: function(e) {
				var index = e.currentTarget.dataset.index;
				console.log(index)
				this.setData({
					pickup_type: parseInt(index),
					startDate: index == 2 ? this.choose_send_time : this.choose_pick_time
				});

				if (index == 1) {
					this.setData({
						canClick: true,
						delivery_fee: 0
					});
				}

				if (index == 2 || index == 3) {
					var that = this;

					if (Object.keys(this.address).length != 0) {
						this.range(function() {
							that.calculateAllMoney();
						});
					} else {
						this.setData({
							canClick: false
						});
					}
				} else {
					this.calculateAllMoney();
				}

				// 重新选择时间
				this.auto_set_time == 1 && this.selectTime()
			},

			//计算钱
			calculateAllMoney() {
				var that = this;
				var payMoney; //需要付的钱

				var tailPrice = 0;
				var coupon = this.coupon;
				var num = this.details.num;
				var details = that.details;
				var delivery_fee = parseFloat(this.delivery_fee); //配送费

				var allMoney = getApp().globalData.roundPrice(details.advance_price * num);
				let actMoney = 0;
				if (that.details) {
					if (that.details.advance_type == 1) {
						//付全款
						payMoney = getApp().globalData.roundPrice(details.advance_price * num);
					} else {
						//付定金
						payMoney = getApp().globalData.roundPrice(details.earnest_price * num);

						if (details.arrive_price) {
							allMoney = getApp().globalData.roundPrice(allMoney - (details.arrive_price - details
								.earnest_price) * num);
						}
					}
				}
				actMoney = allMoney;
				if (coupon) {
					allMoney = getApp().globalData.roundPrice(allMoney - coupon.reduce_money);
				}

				if (that.details.advance_type == 2) {
					tailPrice = getApp().globalData.roundPrice(allMoney - payMoney);
				} else {
					payMoney = allMoney;
				}

				if (this.pickup_type == 2 || this.pickup_type == 3) {
					//商家配送
					if (this.delivery_status == 1 && (actMoney >= parseFloat(this.delivery_cost) || tailPrice >=
							parseFloat(this.delivery_cost) ||
							payMoney >= parseFloat(this.delivery_cost))) {
						delivery_fee = 0
						this.isShowFee = 2;
					} else {
						this.isShowFee = 1;
					}
					allMoney = getApp().globalData.roundPrice(allMoney + delivery_fee);
					tailPrice = tailPrice ? getApp().globalData.roundPrice(tailPrice + delivery_fee) : 0;

					if (that.details.advance_type == 1) {
						payMoney = getApp().globalData.roundPrice(payMoney + delivery_fee);
					}
				}

				that.setData({
					payMoney: payMoney,
					tailPrice: tailPrice,
					allMoney: allMoney
				});
			},
			//选择优惠券
			couponFun: function() {
				console.log(this.details.advance_type, this.allMoney, this.tailPrice, '&goods_id=', this.details
					.goods_id)
				var money = this.details.advance_type == 1 ? this.allMoney : this.tailPrice;
				if (this.isShowFee == 1) {
					money = getApp().globalData.roundPrice(money - this.delivery_fee);
				}
				if (this.coupon) {
					money = getApp().globalData.roundPrice(money + Number(this.coupon.reduce_money));
				}
				//scene_type=2 预售场景
				let sku_id = this.details.sku_id;
				let goods_class_id = this.details.goods_class_id;
				uni.navigateTo({
					url: '../useCoupon/useCoupon?money=' + money + '&goods_id=' + this.details.goods_id +
						'&scene_type=2' + '&sku_id=' + sku_id + '&goods_class_id=' + goods_class_id
				});
			},
			//确认下单
			payBtn: function() {
				if (!this.canClick) {
					return;
				}
				var business_state = this.business_state; //是否开启非营业时间下单
				var send_time = this.business_time;
				if (business_state == 2) {
					let start = send_time[0][0];
					let end = send_time[send_time.length - 1][1];
					let checktime = start + '-' + end;
					if (!getApp().globalData.checkTime(checktime)) {
						uni.showModal({
							title: '提示',
							content: '当前时间不在今日营业时间范围内',
							showCancel: false
						});
						return;
					}
				}

				if (this.pickup_type == 2) {
					var dateTips = '请选择起送时间';
				} else if (this.pickup_type == 1) {
					var dateTips = '请选择自提时间';
				} else if (this.pickup_type != 3) {
					uni.showToast({
						title: '商家暂不支持下单',
						icon: 'none',
						duration: 1000,
						mask: false
					});
					return;
				}

				var startDate = this.startDate;

				if ((startDate == "" || startDate == 'undefined') && this.pickup_type != 3) {
					uni.showToast({
						title: dateTips,
						icon: 'none',
						duration: 1000,
						mask: false
					});
					return;
				}

				var pickup_type = this.pickup_type;
				var address = this.address;
				var link_name = this.link_name;
				var mobile_phone = this.mobile_phone;

				if (pickup_type == 2 || pickup_type == 3) {
					if (Object.keys(this.address).length == 0) {
						uni.showToast({
							title: '请选择收货地址',
							image: "/static/images/tip.png",
							duration: 800,
							mask: true
						});
						return;
					}
				} else {
					if (!link_name || !link_name.length || !link_name.trim().length) {
						uni.showToast({
							title: '请填写姓名',
							image: "/static/images/tip.png",
							duration: 800,
							mask: true
						});
						return;
					}

					var reg = /^1[3456789]\d{9}$/;

					if (!mobile_phone.match(reg)) {
						uni.showToast({
							title: '请完善手机号',
							image: "/static/images/tip.png",
							duration: 800,
							mask: true
						});
						return;
					}
				}

				if (this.details.refuse_earnest_price == 1 && !this.refuse_earnest_price) {
					uni.showToast({
						title: '请您同意不退定金',
						icon: 'none',
						duration: 1500,
						mask: false
					});
					return;
				}

				this.order();
			},
			// 支付下单
			order: function() {
				this.setData({
					canClick: false
				});
				var address_id = this.address.id;
				var coupon = this.coupon;
				var coupon_type = 1;

				if (coupon) {
					var coupon_id = coupon.id;
					coupon_type = coupon.couponType;
				} else {
					var coupon_id = '';
					coupon_type = '';
				}

				var that = this;
				var thisYears = new Date().getFullYear();
				var pickup_type = this.pickup_type;

				if (pickup_type == 1) {
					address_id = '';
				}

				var startDate = this.startDate; //时间

				if (pickup_type == 3) {
					startDate = '';
				} else {
					startDate = startDate.replace("月", "-");
					startDate = startDate.replace("日", " ");
					startDate = thisYears + "-" + startDate + ":00";
				}

				uni.showLoading({
					title: '提交订单中...',
					// #ifndef MP-ALIPAY
					mask: true,
					// #endif
				});
				var delivery_fee = this.delivery_fee;
				getApp().globalData.sendRequest({
					url: '/Advance/advance_place_order_v1',
					type: "post",
					showLoading: false,
					data: {
						advance_id: this.details.id,
						all_money: this.allMoney,
						address_id: address_id,
						pickup_type: pickup_type,
						coupon_id: coupon_id,
						coupon_type: coupon_type,
						mobile_phone: that.mobile_phone,
						link_name: that.link_name,
						delivery_fee: delivery_fee,
						remark: that.remark,
						goods_id: that.details.goods_id,
						num: that.details.num,
						sku_id: that.details.sku_id,
						start_date: startDate
					},
					success: function(res) {
						uni.hideLoading();
						if (res.status == 1) {
							uni.redirectTo({
								url: '/pages/order/waitPay/waitPay?order_no=' + res.data.order_no +
									'&money=' + res.data.allMoney
							});
						} else {
							uni.showToast({
								title: res.msg,
								icon: 'none',
								duration: 1500
							});
							that.setData({
								canClick: true
							});
						}
					}
				});
			},
			//获取配送费
			range: function(fn) {
				var that = this;
				var address = this.address;
				if(!address.id){
					typeof fn == "function" && fn();
					return;
				}
				getApp().globalData.sendRequest({
					url: '/Address/range_v2',
					type: "post",
					data: {
						advance_id: this.details.id,
						address_id: address.id,
						order_genre: 4,
						//预售
						pickup_type: this.pickup_type,
						num: this.details.num,
						sku_id: this.details.sku_id ? this.details.sku_id : 0,
					},
					success: function(res) {
						if (res.status == 1) {
							that.setData({
								delivery_fee: res.fee,
								distance: that.pickup_type == 2 ? res.distance : 0,
								canClick: true
							});

							typeof fn == "function" && fn();
						} else {
							that.setData({
								canClick: false,
								delivery_fee: 0,
								distance: res.distance ? res.distance : 0
							});
							uni.showToast({
								title: res.msg ? ('地址: ' + res.msg) : '收货地址不在配送范围内',
								icon: 'none',
								duration: 1500,
								mask: false
							});

							typeof fn == "function" && fn();
						}
					}
				});
			},
			//点击时间选择器
			pickerTap: function() {
				console.log(this.business_time, 'this.business_time')
				this.setData({
					cacul_time: this.pickup_type == 2 ? app.handleTimeArr(this.send_time) : app.handleTimeArr(
						this.business_time)
				})
				this.loadMonthDate();
				this.loadHours();
				this.loadMinutes();
			},
			//修改时间选择器的列
			bindMultiPickerColumnChange: function(e) {
				var multiIndex = this.multiIndex; // 把选择的对应值赋值给 multiIndex
				multiIndex[e.detail.column] = e.detail.value; // 然后再判断当前改变的是哪一列,如果是第1列改变
				if (e.detail.column === 0) {
					multiIndex[1] = 0;
					multiIndex[2] = 0;
					this.setData({
						multiIndex: multiIndex
					});
					this.loadHours();
					this.loadMinutes();
				} else if (e.detail.column === 1) {
					// 如果是第2列改变
					multiIndex[2] = 0;
					this.setData({
						multiIndex: multiIndex
					});
					this.loadMinutes();
				}
			},
			//确认修改时间
			bindStartMultiPickerChange: function(e) {
				var monthDay = this.multiArray[0][e.detail.value[0]];
				var hours = this.multiArray[1][e.detail.value[1]];
				var minute = this.multiArray[2][e.detail.value[2]];

				if (monthDay === "今天") {
					var month = new Date().getMonth() + 1;
					var day = new Date().getDate();
					monthDay = month + "月" + day + "日";
				} else if (monthDay === "明天") {
					var date1 = new Date();
					date1.setDate(new Date().getDate() + 1);
					monthDay = date1.getMonth() + 1 + "月" + date1.getDate() + "日";
				} else {
					var month = monthDay.split("-")[0]; // 返回月
					var day = monthDay.split("-")[1]; // 返回日
					monthDay = month + "月" + day + "日";
				}
				var startDate = monthDay + " " + hours + ":" + minute;
				var obj = {
					startDate: startDate,
					multiIndex: e.detail.value
				};

				if (this.pickup_type == 2) {
					//配送
					obj.choose_send_time = startDate;
				} else {
					//自提
					obj.choose_pick_time = startDate;
				}
				this.setData(obj);
			},

			dealesTime: function(es_time) {
				const delay_time = this.pickup_type == 2 ? this.send_delayed_time * 60 : this.pickup_delayed_time * 60
				if (delay_time > es_time) {
					es_time = delay_time
				} else if (es_time < 0) {
					es_time = 0
				}

				return es_time
			},

			//生成可选月日
			loadMonthDate: function() {
				let thisTime = Date.parse(new Date()) / 1000;
				// this.details.deliver_s_time = 1622443821
				let es_time = app.roundPrice(this.details.deliver_s_time - thisTime); //往后延长的开始时间的秒数
				const res_time = this.dealesTime(es_time)
				console.log(res_time, 'res_time')
				//结束时间距离当前的天数 由于预售的发货时间只选择到日，所以加上一天便于计算
				let end_day = parseInt(app.roundPrice(this.details.deliver_e_time - thisTime) / 86400) + 1;
				console.log(end_day, 'end_day')

				var multiArray = this.multiArray;
				multiArray[0] = app.createMonthDay(this.cacul_time, res_time, end_day);
				this.setData({
					multiArray: multiArray
				});
				this.$forceUpdate()
			},
			//生成可选时
			loadHours: function() {
				let thisTime = Date.parse(new Date()) / 1000;
				let es_time = app.roundPrice(this.details.deliver_s_time - thisTime); //往后延长的开始时间的秒数
				const res_time = this.dealesTime(es_time)
				var multiArray = this.multiArray;
				multiArray[1] = app.createHours(this.cacul_time, this.multiArray, this.multiIndex, res_time);
				this.setData({
					multiArray: multiArray
				});
				this.$forceUpdate()
			},
			//生成可选分
			loadMinutes() {
				let thisTime = Date.parse(new Date()) / 1000;
				let es_time = app.roundPrice(this.details.deliver_s_time - thisTime); //往后延长的开始时间的秒数
				const res_time = this.dealesTime(es_time)
				var multiArray = this.multiArray;
				multiArray[2] = app.createMinutes(this.cacul_time, multiArray, this.multiIndex, res_time);
				this.setData({
					multiArray: multiArray
				});
				this.$forceUpdate()
			},


			// 完成时间筛选框
			successSelect() {
				this.$refs.popup.close()
			},

			// 支付宝小程序选择时间
			showSelectTime() {
				this.$refs.popup.open('bottom')
			},
		}
	};
</script>
<style>
	@import "./advanceSettlement.css";

	.flex {
		display: flex;
		justify-content: space-between;
		width: 100%;

	}

	.zhufu_bg {
		width: 100%;
		height: 200rpx;
	}

	.zhufu_bg_now {
		width: 100%;
		height: 100rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;
		box-sizing: border-box;
		padding: 0 30rpx;
	}

	.play_pic {
		width: 40rpx;
		height: 40rpx;
		display: inline-block;
		margin-right: 20rpx;
	}

	.play_icon {
		display: flex;
		align-items: center;

	}

	.blesstitle {
		display: flex;
		flex-direction: column;

	}

	.blessttext {
		font-size: 24rpx;
		color: #333;
	}

	.livePlay {
		position: absolute;
		top: 50%;
		left: 50%;
		width: 600rpx;
		height: 600rpx;
		transform: translate(-50%, -50%);
		z-index: 99;
		display: flex;
		flex-direction: column;

	}

	.closeIcon {
		flex: 1;
		magrin-bottom: 10rpx;
		display: flex;
		justify-content: flex-end;
	}

	/* .lineH {
		line-height: 48rpx;
	} */
</style>

<style lang="less" scoped>
	.settlement-list-choose {
		.select-time {
			color: #ff3d3d;
		}

		/deep/.uni-popup {
			background-color: #fff;

			.uni-transition {
				background-color: #fff;

				.uni-popup__wrapper-box {
					background-color: #fff;

					.operate {
						display: flex;
						justify-content: flex-end;
						align-items: center;
						padding: 10px 20px 0 20px;
						position: absolute;
						z-index: 9999;
						width: 100%;
						box-sizing: border-box;

						.sure {
							color: #07c160;
						}
					}
				}
			}
		}
	}
</style>
